//
//  ST_TianNumberBadgeButton.swift
//  btvideo
//
//  Created by Jason on 2021/9/17.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit

class ST_TianNumberBadgeButton: ST_TianButton {
    
    
    init(style:ST_TianButtonStyle,badgeCenterXOffset:CGFloat,badgeTopOffset:CGFloat,badgeHeight:CGFloat = 16){
        super.init(style: style)
        
        self.addSubview(badgeLabel)
        badgeLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview().offset(badgeCenterXOffset)
            make.top.equalToSuperview().offset(badgeTopOffset)
            make.height.equalTo(badgeHeight)
            make.width.equalTo(18)
        }
        badgeLabel.cornerRadius = badgeHeight/2.0
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var badgeLabel:UILabel = {
        let l = UILabel()
        l.textColor = .white
        l.backgroundColor = #colorLiteral(red: 0.9215686275, green: 0.2352941176, blue: 0.2352941176, alpha: 1)
        l.font = .systemFont(ofSize: 12, weight: .medium)
        l.textAlignment = .center
        return l
    }()
    
}



extension ST_TianNumberBadgeButton{
    
    func setBadge(number:Int){
        
        if number > 99{
            badgeLabel.text = "99+"
            badgeLabel.snp.updateConstraints { (make) in
                make.width.equalTo(30)
            }
        }else if number > 0 {
            badgeLabel.text = number.string
            badgeLabel.snp.updateConstraints { (make) in
                make.width.equalTo(number > 9 ? 24 : 18)
            }
        }else{
            badgeLabel.isHidden = true
        }
        
    }
    
}
